//reqHandler.js
var fs = require('fs');
var querystring = require('querystring');
function reqHandler(res,req){
	this.start = function (res){
		var pageContent = fs.readFileSync("index.html","utf8");
		res.writeHead(200,{"Content-Type":"text/html"});
		res.end(pageContent);
	}
	this.show = function(res){
		fs.readFile("./111.jpg", "binary" , function (error,file) {
			if(error){
				res.writeHead(500 , {"Content-Type":"text/html"});
				res.write(error + "\n");
				res.end();
			}else{
				res.writeHead(200 , {"Content-Type":"image/jpg"});
				res.write(file,'binary');
				res.end();
			}
		});
	}
	this.form = function(res){
		var pageContent = fs.readFileSync("form.html","utf8");
		res.writeHead(200,{"Content-Type":"text/html"});
		res.end(pageContent);
	}
	this.post = function(res,req){
		var post = '';
		req.on("data",function(chunk){
			post+= chunk;
		});
		req.on("end",function(){
			var name = querystring.parse(post).name;
			var email = querystring.parse(post).email;
			var address = querystring.parse(post).address;
			res.writeHead(200,{"Content-Type":"text/html"});
			res.write("<meta charset=\"utf-8\"/>");
			res.write("<h3 style=\"color:red\">你提交的信息如下：<h3/>");
			res.write("Your name is : "+ name + "<br/>");
			res.write("Your email is : "+ email+ "<br/>");
			res.write("Your address is : "+ address+ "<br/>");
			res.end();
		});
	}
	this._404 = function(res){
		res.writeHead(200,{"Content-Type":"text/html"});
		res.end("404 Not Found");
	}
}
module.exports = reqHandler;